package com.company.oa.meeting.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.common.utils.SecurityUtils;
import com.company.oa.meeting.dto.MeetingDTO;
import com.company.oa.meeting.entity.Meeting;
import com.company.oa.meeting.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 会议控制器
 */
@RestController
@RequestMapping("/meetings")
public class MeetingController {

    @Autowired
    private MeetingService meetingService;

    /**
     * 分页查询会议
     */
    @GetMapping("/page")
    public Result<IPage<Meeting>> getMeetingPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Meeting meeting) {
        IPage<Meeting> pageResult = meetingService.getMeetingPage(page, size, meeting);
        return Result.success(pageResult);
    }

    /**
     * 获取会议详情
     */
    @GetMapping("/{id}")
    public Result<Meeting> getMeetingDetail(@PathVariable Long id) {
        Meeting meeting = meetingService.getMeetingDetail(id);
        if (meeting == null) {
            return Result.failed("会议不存在");
        }
        return Result.success(meeting);
    }

    /**
     * 创建会议
     */
    @PostMapping
    public Result<Boolean> createMeeting(@RequestBody @Valid MeetingDTO meetingDTO) {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        Long deptId = SecurityUtils.getDeptId();
        String deptName = SecurityUtils.getDeptName();
        
        boolean result = meetingService.createMeeting(meetingDTO, userId, userName, deptId, deptName);
        return Result.success(result);
    }

    /**
     * 修改会议
     */
    @PutMapping
    public Result<Boolean> updateMeeting(@RequestBody @Valid MeetingDTO meetingDTO) {
        if (meetingDTO.getId() == null) {
            return Result.failed("会议ID不能为空");
        }
        boolean result = meetingService.updateMeeting(meetingDTO);
        return Result.success(result);
    }

    /**
     * 取消会议
     */
    @PostMapping("/{id}/cancel")
    public Result<Boolean> cancelMeeting(@PathVariable Long id) {
        boolean result = meetingService.cancelMeeting(id);
        return Result.success(result);
    }

    /**
     * 查询我组织的会议
     */
    @GetMapping("/my-organized")
    public Result<IPage<Meeting>> getMyOrganizedMeetings(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Long userId = SecurityUtils.getUserId();
        IPage<Meeting> pageResult = meetingService.getMyOrganizedMeetings(page, size, userId);
        return Result.success(pageResult);
    }

    /**
     * 查询我参与的会议
     */
    @GetMapping("/my-participated")
    public Result<IPage<Meeting>> getMyParticipatedMeetings(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Long userId = SecurityUtils.getUserId();
        IPage<Meeting> pageResult = meetingService.getMyParticipatedMeetings(page, size, userId);
        return Result.success(pageResult);
    }

    /**
     * 确认参加会议
     */
    @PostMapping("/{id}/confirm")
    public Result<Boolean> confirmMeeting(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        boolean result = meetingService.confirmMeeting(id, userId);
        return Result.success(result);
    }

    /**
     * 拒绝参加会议
     */
    @PostMapping("/{id}/reject")
    public Result<Boolean> rejectMeeting(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        boolean result = meetingService.rejectMeeting(id, userId);
        return Result.success(result);
    }

    /**
     * 获取会议统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getMeetingStatistics() {
        Long userId = SecurityUtils.getUserId();
        Map<String, Object> statistics = meetingService.getMeetingStatistics(userId);
        return Result.success(statistics);
    }
}